import pandas as pd
import numpy as np
import sweetviz as sv

# 读取数据
data_path ="/data/wangxiaoshi/EvolveGCN/data/trendingweibo/trending_value3.csv"
df = pd.read_csv(data_path)

# 基础清洗 -------------------------------------------------
# 强制处理label列
df['label'] = pd.to_numeric(df['label'], errors='coerce')
df = df.dropna(subset=['label'])  # 必须保证label无缺失

# 处理无限值（关键步骤）
numeric_cols = df.select_dtypes(include=[np.number]).columns
df[numeric_cols] = df[numeric_cols].replace([np.inf, -np.inf], np.nan)

# 宽松化处理其他特征缺失（不中断分析）
print("\n各数值列缺失值统计：")
print(df[numeric_cols].isnull().sum())

# 生成报告（带容错机制）-------------------------------------
try:
    report = sv.analyze(
        df,
        target_feat='label',
        feat_cfg=sv.FeatureConfig(
            skip=["id", "timestamp"],
            force_num=numeric_cols.drop('label').tolist()  # 明确数值类型
        ),
        pairwise_analysis='auto'
    )
    report.show_html('analysis_with_missing.html')
    print("\n报告生成成功，已自动忽略特征列的缺失值")

except Exception as e:
    print(f"\n分析中断，原因: {str(e)}")
    # 定位问题列
    problem_col = [col for col in numeric_cols if not np.isfinite(df[col]).all()]
    print(f"建议检查以下列的异常值: {problem_col}")

# 强制生成简化报告（保底方案）
finally:
    if 'report' not in locals():
        print("\n生成简化版报告...")
        safe_cols = ['label'] + df[numeric_cols].dropna(axis=1).columns.tolist()
        sv.analyze(df[safe_cols], target_feat='label').show_html('safe_report.html')